package com.wmlive.hhvideo.heihei.bells.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wmlive.hhvideo.heihei.beans.bell.BellMessage;
import com.wmlive.hhvideo.heihei.beans.bell.MessageType;
import com.wmlive.hhvideo.heihei.bells.callback.BellMessageClickCallBack;
import com.wmlive.hhvideo.utils.CollectionUtil;
import com.wmlive.hhvideo.utils.DeviceUtils;
import com.wmlive.hhvideo.utils.IMTimeUtils;
import com.wmlive.hhvideo.utils.imageloader.GlideLoader;
import com.wmlive.hhvideo.widget.refreshrecycler.BaseRecyclerViewHolder;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshAdapter;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wmlive.hhvideo.R;

/**
 * Created by vhawk on 2017/7/5.
 */

public class BellAdapter extends RefreshAdapter<BellAdapter.BellViewHolder, BellMessage> {

    private BellMessageClickCallBack clickCallBack;
    private Context context;

    public BellAdapter(List<BellMessage> list, RefreshRecyclerView refreshView) {
        super(list, refreshView);
    }

    @Override
    public BellViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new BellViewHolder(parent, R.layout.item_bell);
    }

    @Override
    public void onBindHolder(BellViewHolder holder, int position, BellMessage data) {
        MessageType messageType = MessageType.getMessageTypeByType(data.getMsg_type());

        if (messageType == null) {
            return;
        }

        initCommonUI(holder, data);

        switch (messageType) {

            case MESSAGE_TYPE_FOLLOW:
                showFollowView(holder, data);
                break;

            case MESSAGE_TYPE_LIKE:
                showLoveView(holder, data);
                break;

            case MESSAGE_TYPE_COMMENT:
                showCommentView(holder, data);
                break;

            case MESSAGE_TYPE_SYSTEM:
                showOfficialNewsView(holder, data);
                break;

            default:

                showUnSupportMessage(holder, data);
                break;
        }

    }

    /**
     * 不支持消息类型
     *
     * @param holder
     * @param data
     */
    private void showUnSupportMessage(BellViewHolder holder, BellMessage data) {
        if (!TextUtils.isEmpty(data.getOtherName())) {
            holder.tvNickName.setText(data.getOtherName());
        }
        holder.tvContent.setVisibility(View.VISIBLE);
        holder.ivFollow.setVisibility(View.GONE);
        holder.tvGetInfo.setVisibility(View.GONE);
        holder.ivThumbnail.setVisibility(View.GONE);
        holder.ivSystemMessage.setVisibility(View.GONE);
        holder.tvAnnouncement.setVisibility(View.GONE);
//        GlideLoader.loadCornerImage(data.getThumbnail(), holder.ivThumbnail, R.drawable.bg_bell_video, DeviceUtils.dip2px(context, 6));
        if (!TextUtils.isEmpty(data.getDesc())) {
            holder.tvContent.setText("该版本不支持的消息类型，请升级到最新版本");
            holder.tvContent.setTextColor(context.getResources().getColor(R.color.hh_color_a));
        }
    }

    /**
     * 消息列表公共UI
     *
     * @param holder
     * @param bellMessage
     */
    private void initCommonUI(BellViewHolder holder, BellMessage bellMessage) {

        String title = bellMessage.getTitle();
        if (!TextUtils.isEmpty(title)) {
            holder.tvAction.setText(title);
        } else {
            // TODO: 2017/7/6  
        }

        GlideLoader.loadCircleImage(bellMessage.getProfile(), holder.ivProfile, R.drawable.find_bg_user_default);

        holder.tvTime.setText(IMTimeUtils.getBellData(1000 * bellMessage.getCreate_time()));
    }


    /**
     * 官方消息
     *
     * @param holder
     * @param data
     */
    private void showOfficialNewsView(BellViewHolder holder, BellMessage data) {
        holder.ivFollow.setVisibility(View.GONE);
        holder.tvAction.setText("");
        holder.tvNickName.setText(data.getTitle());

        if (TextUtils.isEmpty(data.getContentIcon())) {
            holder.ivSystemMessage.setVisibility(View.GONE);
        } else {
            holder.ivSystemMessage.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(data.getJumpIcon())) {
            holder.ivThumbnail.setVisibility(View.GONE);
        } else {
            holder.ivThumbnail.setVisibility(View.VISIBLE);
            GlideLoader.loadCornerImage(data.getJumpIcon(), holder.ivThumbnail, R.drawable.bg_bell_video, DeviceUtils.dip2px(context, 6));
        }

        if (!TextUtils.isEmpty(data.getDesc())) {
            holder.tvContent.setText(data.getDesc());
            holder.tvContent.setTextColor(context.getResources().getColor(R.color.hh_color_b));
        }
        String text = data.getOfficialText();
        if (TextUtils.isEmpty(text)) {
            holder.tvGetInfo.setVisibility(View.GONE);
        } else {
            holder.tvGetInfo.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(text) && TextUtils.isEmpty(data.getJumpIcon())) {
            holder.tvAnnouncement.setVisibility(View.VISIBLE);
            holder.tvAnnouncement.setText(data.getDesc());
            holder.tvContent.setVisibility(View.GONE);
        } else {
            holder.tvContent.setVisibility(View.VISIBLE);
            holder.tvAnnouncement.setVisibility(View.GONE);
        }
    }

    /**
     * 评论回复
     *
     * @param holder
     * @param data
     */
    private void showCommentView(BellViewHolder holder, BellMessage data) {
        holder.tvNickName.setText(data.getOtherName());
        holder.tvContent.setVisibility(View.VISIBLE);
        holder.ivFollow.setVisibility(View.GONE);
        holder.tvGetInfo.setVisibility(View.GONE);
        holder.ivThumbnail.setVisibility(View.VISIBLE);
        holder.tvContent.setVisibility(View.VISIBLE);
        holder.ivSystemMessage.setVisibility(View.GONE);
        holder.tvAnnouncement.setVisibility(View.GONE);
        GlideLoader.loadCornerImage(data.getThumbnail(), holder.ivThumbnail, R.drawable.bg_bell_video, DeviceUtils.dip2px(context, 6));
        if (!TextUtils.isEmpty(data.getDesc())) {
            holder.tvContent.setText(data.getDesc());
            holder.tvContent.setTextColor(context.getResources().getColor(R.color.hh_color_a));
        }
    }

    /**
     * 赞了你的作品
     *
     * @param holder
     * @param data
     */
    private void showLoveView(BellViewHolder holder, BellMessage data) {
        holder.tvNickName.setText(data.getOtherName());
        holder.ivFollow.setVisibility(View.GONE);
        holder.tvGetInfo.setVisibility(View.GONE);
        holder.ivThumbnail.setVisibility(View.VISIBLE);
        holder.tvContent.setVisibility(View.GONE);
        holder.ivSystemMessage.setVisibility(View.GONE);
        holder.tvAnnouncement.setVisibility(View.GONE);
        GlideLoader.loadCornerImage(data.getThumbnail(), holder.ivThumbnail, R.drawable.bg_bell_video, DeviceUtils.dip2px(context, 6));
    }

    /**
     * 关注UI
     *
     * @param holder
     * @param bellMessage
     */
    private void showFollowView(BellViewHolder holder, BellMessage bellMessage) {
        holder.ivFollow.setVisibility(View.VISIBLE);
        holder.tvGetInfo.setVisibility(View.GONE);
        holder.ivThumbnail.setVisibility(View.GONE);
        holder.ivSystemMessage.setVisibility(View.GONE);
        holder.tvAnnouncement.setVisibility(View.GONE);
        holder.tvNickName.setText(bellMessage.getOtherName());

        if (TextUtils.isEmpty(bellMessage.getDesc())){
            holder.tvContent.setVisibility(View.GONE);

        }else {
            holder.tvContent.setVisibility(View.VISIBLE);
            holder.tvContent.setText(bellMessage.getDesc());
            holder.tvContent.setTextColor(context.getResources().getColor(R.color.hh_color_b));
        }

        if (bellMessage.isFollowAuthor()) {
            holder.ivFollow.setImageResource(R.drawable.icon_friend_check_nor);
        } else {
            holder.ivFollow.setImageResource(R.drawable.icon_profile_other_add);
        }

    }

    public void setClickCallBack(BellMessageClickCallBack clickCallBack) {
        this.clickCallBack = clickCallBack;
    }

    /**
     * 关注成功回调
     *
     * @param position
     */
    public void followSucceed(int position) {
        // TODO: 2017/7/6
        BellMessage bellMessage = getDataContainer().get(position);
        bellMessage.getFrom_user().getRelation().setIs_follow(true);
        notifyItemChanged(position);
    }

    /**
     * 取消关注成功回调
     *
     * @param position
     */
    public void unFollowSucceed(int position) {
        // TODO: 2017/7/6
        BellMessage bellMessage = getDataContainer().get(position);
        bellMessage.getFrom_user().getRelation().setIs_follow(false);
        notifyItemChanged(position);
    }

    public class BellViewHolder extends BaseRecyclerViewHolder implements View.OnClickListener {

        @BindView(R.id.iv_profile)
        public ImageView ivProfile;
        @BindView(R.id.tv_nick_name)
        public TextView tvNickName;
        @BindView(R.id.tv_action)
        public TextView tvAction;
        @BindView(R.id.tv_content)
        public TextView tvContent;
        @BindView(R.id.tv_time)
        public TextView tvTime;
        @BindView(R.id.iv_thumbnail)
        public ImageView ivThumbnail;
        @BindView(R.id.iv_follow)
        public ImageView ivFollow;
        @BindView(R.id.tv_get_info)
        public TextView tvGetInfo;
        @BindView(R.id.iv_system_message)
        public ImageView ivSystemMessage;
        @BindView(R.id.tv_system_announcement)
        public TextView tvAnnouncement;


        public BellViewHolder(ViewGroup parent, int layoutId) {
            super(parent, layoutId);
//            setItemClickListener(this);
            itemView.setOnClickListener(this);
        }

        /**
         * 头像点击
         */
        @OnClick(R.id.iv_profile)
        public void onProfileClick() {
            if (!CollectionUtil.isEmpty(getDataContainer()) && getLayoutPosition() > -1 && getLayoutPosition() < getDataContainer().size()) {
                BellMessage bellMessage = getDataContainer().get(getLayoutPosition());
                MessageType messageType = MessageType.getMessageTypeByType(bellMessage.getMsg_type());

                if (messageType == null) {
                    return;
                }
                switch (messageType) {
                    case MESSAGE_TYPE_FOLLOW:
                    case MESSAGE_TYPE_LIKE:
                    case MESSAGE_TYPE_COMMENT:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToAuthorHomePage(bellMessage.getUserId(), bellMessage.getMsg_id());
                        }
                        break;

                    case MESSAGE_TYPE_SYSTEM:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToActivity(bellMessage.getLink(), bellMessage.getMsg_id());
                        }
                        break;

                    default:
                        break;
                }
            }

        }

        /**
         * 昵称点击
         */
        @OnClick(R.id.tv_nick_name)
        public void onNickNameClick() {
            if (!CollectionUtil.isEmpty(getDataContainer()) && getLayoutPosition() > -1 && getLayoutPosition() < getDataContainer().size()) {
                BellMessage bellMessage = getDataContainer().get(getLayoutPosition());
                MessageType messageType = MessageType.getMessageTypeByType(bellMessage.getMsg_type());

                if (messageType == null) {
                    return;
                }
                switch (messageType) {
                    case MESSAGE_TYPE_FOLLOW:
                    case MESSAGE_TYPE_LIKE:
                    case MESSAGE_TYPE_COMMENT:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToAuthorHomePage(bellMessage.getUserId(), bellMessage.getMsg_id());
                        }
                        break;

                    case MESSAGE_TYPE_SYSTEM:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToActivity(bellMessage.getLink(), bellMessage.getMsg_id());
                        }
                        break;

                    default:
                        break;
                }
            }
        }

        /**
         * 关注按钮点击
         */
        @OnClick(R.id.iv_follow)
        public void onFollowClick() {
            if (!CollectionUtil.isEmpty(getDataContainer()) && getLayoutPosition() > -1 && getLayoutPosition() < getDataContainer().size()) {
                BellMessage bellMessage = getDataContainer().get(getLayoutPosition());
                MessageType messageType = MessageType.getMessageTypeByType(bellMessage.getMsg_type());

                if (messageType == null) {
                    return;
                }

                switch (messageType) {
                    case MESSAGE_TYPE_FOLLOW:
                        if (clickCallBack != null) {
                            if (!bellMessage.isFollowAuthor()) {
                                clickCallBack.followAuthor(bellMessage.getUserId(), getLayoutPosition(), bellMessage.getMsg_id());
                            } else {
                                clickCallBack.unFollowAuthor(bellMessage.getUserId(), getLayoutPosition(), bellMessage.getMsg_id());
                            }
                        }

                        break;
                    default:

                        break;
                }

            }
        }

        /**
         * 视频缩略图点击
         */
        @OnClick(R.id.iv_thumbnail)
        public void onThumbnailClick() {
            if (!CollectionUtil.isEmpty(getDataContainer()) && getLayoutPosition() > -1 && getLayoutPosition() < getDataContainer().size()) {
                BellMessage bellMessage = getDataContainer().get(getLayoutPosition());
                MessageType messageType = MessageType.getMessageTypeByType(bellMessage.getMsg_type());

                if (messageType == null) {
                    return;
                }

                switch (messageType) {
                    case MESSAGE_TYPE_LIKE:
                    case MESSAGE_TYPE_COMMENT:
                    case MESSAGE_TYPE_SYSTEM:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToActivity(bellMessage.getLink(), bellMessage.getMsg_id());
                        }
                        break;
                    default:
                        break;
                }

            }
        }

        /**
         * 官方消息，查看详情点击
         */
        @OnClick(R.id.tv_get_info)
        public void onGetInfoClick() {
            if (!CollectionUtil.isEmpty(getDataContainer()) && getLayoutPosition() > -1 && getLayoutPosition() < getDataContainer().size()) {
                BellMessage bellMessage = getDataContainer().get(getLayoutPosition());
                MessageType messageType = MessageType.getMessageTypeByType(bellMessage.getMsg_type());

                if (messageType == null) {
                    return;
                }
                switch (messageType) {
                    case MESSAGE_TYPE_SYSTEM:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToActivity(bellMessage.getLink(), bellMessage.getMsg_id());
                        }
                        break;

                    default:
                        break;
                }
            }
        }


        @Override
        public void onClick(View v) {
            // 条目点击
            if (!CollectionUtil.isEmpty(getDataContainer()) && getLayoutPosition() > -1 && getLayoutPosition() < getDataContainer().size()) {
                BellMessage bellMessage = getDataContainer().get(getLayoutPosition());
                MessageType messageType = MessageType.getMessageTypeByType(bellMessage.getMsg_type());

                if (messageType == null) {
                    return;
                }
                switch (messageType) {
                    case MESSAGE_TYPE_FOLLOW:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToAuthorHomePage(bellMessage.getUserId(), bellMessage.getMsg_id());
                        }
                        break;

                    case MESSAGE_TYPE_LIKE:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToAuthorHomePage(bellMessage.getUserId(), bellMessage.getMsg_id());
                        }
                        break;

                    case MESSAGE_TYPE_COMMENT:
                        if (clickCallBack != null) {
                            clickCallBack.replyComment(bellMessage.getUserId(), bellMessage.getVideoId(), bellMessage.getOtherName(), bellMessage.getMsg_id());
                        }
                        break;

                    case MESSAGE_TYPE_SYSTEM:
                        if (clickCallBack != null) {
                            clickCallBack.navigateToActivity(bellMessage.getLink(), bellMessage.getMsg_id());
                        }
                        break;

                    default:
                        break;
                }
            }
        }
    }
}
